package src.Server;
import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;

import src.common.*;
import src.common.impl.Message;

public class ServerReceiverThread extends Thread{

    Socket s;

    public ServerReceiverThread (Socket s) {
        this.s=s;
    }

    public void run(){//定义线程方法

        while(true){//在线程中不断接收客户端发送来的信息

            try {

//创建对象输入流对象
                ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
                Message mess=(Message) ois.readObject () ; // Message X

                if (mess.getMessageType().equals(MessageType.USER_EXIT_SERVER_THREAD_CLOSE)){
                    String sender = mess.getSender();
                    mess.setMessageType(MessageType.USER_EXIT_CLIENT_THREAD_CLOSE);
                    sendMessage(s,mess);
                    System.out.println(sender+"退出了!服务器关闭他的线程");
                    s.close();
                    break;
                }


                if (mess.getMessageType().equals(MessageType.ADD_NEW_FRIEND)){
                    String sender = mess.getSender();
                    String newFriend = mess.getContent();

                    if (DBUtil.seekUser(newFriend)){
                        if(DBUtil.seekFriend(sender,newFriend,1)){
                            mess.setMessageType(MessageType.ADD_NEW_FRIEND_FAILURE_ALREADY_EXITS);
                        }else {
                            DBUtil.addFriend(sender,newFriend,1);
                            String allFriends = DBUtil.seekAllFriends(sender, 1);
                            mess.setContent(allFriends);
                            mess.setMessageType(MessageType.ADD_NEW_FRIEND_SUCCESS);
                        }
                    }else {
                        mess.setMessageType(MessageType.ADD_NEW_FRIEND_FAILURE_NO_UERR);
                    }

                    Socket ss = (Socket) YYChatServer.hmSocket.get(sender);
                    sendMessage(ss,mess);
                }

                if (mess.getMessageType () .equals (MessageType. COMMON_CHAT_MESSAGE) ) {

                    System.out.println (mess.getSender ()+" 对 "+mess.getReceiver ()+
                             "说:"+mess.getContent ());

                    mess.setSendTime(new Date());

                    DBUtil.saveMessage(mess);

                    String receiver = mess.getReceiver();
                    Socket rs = (Socket) YYChatServer.hmSocket.get(receiver);
                    System.out.println("接收方"+receiver+"的socket对象"+rs);
                    if (rs!= null) {
                        ObjectOutputStream oos = new ObjectOutputStream(rs.getOutputStream());
                        oos.writeObject (mess);
                    }else {
                        System.out.println("接收方"+receiver+"不在线");
                    }
                }

                if (mess.getMessageType () .equals (MessageType.REQUEST_ONLINE_FRIENDS) ){
                    Set OnlineFriendSet = YYChatServer.hmSocket.keySet();
                    Iterator it = OnlineFriendSet.iterator();
                    String onlineFriends = "";
                    while (it.hasNext()) {
                        onlineFriends = " "+(String) it.next()+onlineFriends;
                    }
                    mess.setReceiver(mess.getSender());
                    mess.setSender("Server");
                    mess.setMessageType(MessageType.RESPONSE_ONLINE_FRIENDS);
                    mess.setContent(onlineFriends);
                    sendMessage(s,mess);
                }

                if (mess.getMessageType () .equals (MessageType.NEW_ONLINE_TO_ALL_FRIEND) ){
                    mess.setMessageType(MessageType.NEW_ONLINE_FRIEND);
                    Set onlineFriendSet = YYChatServer.hmSocket.keySet();
                    Iterator it = onlineFriendSet.iterator();
                    while (it.hasNext()) {
                        String receiver = (String) it.next();
                        mess.setReceiver(receiver);
                        Socket rs = (Socket) YYChatServer.hmSocket.get(receiver);
                        sendMessage(rs,mess);
                    }
                }
            }catch (ClassNotFoundException | IOException e) {
                e.printStackTrace () ;

            }
        }

    }
    public void sendMessage(Socket s, Message mess) {

        ObjectOutputStream oos;

        try {

            oos = new ObjectOutputStream(s.getOutputStream());

            oos.writeObject(mess);//发送聊天信息

        } catch (IOException e) {

            e.printStackTrace () ;

        }

    }
}